import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    companyList: [],
    companyDown: []
  },
  mutations: {
    companyListTo(state, data) {
      state.companyList = data
    },
    companyDownTo(state, data) {
      state.companyDown = data
    },
  },
  actions: {
    getCompanyList({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getCompanyList,
        data: {
          current: that.current,
          pageSize: that.pageSize,
          wuqTitle: that.wuqTitle
        }
      }).then(res => {
        commit('companyListTo', res.data.records)
        that.total = res.data.total
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    getCompanyDown({commit}, that) {
      console.log("getCompanyDown")
      that.$axios({
        method: 'post',
        url: Api.getCompanyList,
        data: {
          current: 1,
          pageSize: 100   ,
          wuqTitle: that.wuqTitle
        }
      }).then(res => {
        commit('companyDownTo', res.data.records)
      }).catch(() => {
      })
    },
    addCompany({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addCompany,
        data: that.companyForm
      }).then(res => {
        if (res.code === 20002) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.current = 1
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    editCompany({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.editCompany,
        data: that.companyForm
      }).then(res => {
        if (res.code === 20003) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    removeCompany({state}, that) {
      let url = Api.removeCompany.replace('{wuqId}', that.wuqId)
      return that.$axios({
        method: 'get',
        url: url
      }).then(res => {
        if (res.code === 20004) {
          that.delVisible = false
          that.getData()
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    rechargeCompany({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.rechargeCompany,
        data: that.rechargeForm
      }).then(res => {
        if (res.code === 20000) {
          that.rechargeVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    }
  }
}
